package be.kuleuven.thesis.neo4j;

import java.io.IOException;

import org.biojava3.genome.parsers.gff.FeatureI;
import org.biojava3.genome.parsers.gff.FeatureList;
import org.biojava3.genome.parsers.gff.GFF3Reader;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
//import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;


public class ImportGenes {
	private static Neo4jDBManager dbManager = new Neo4jDBManager();
	private static final String GENE_KEY = "gene";
	private static final String LOCATION_KEY = "location";
	private static Label geneLabel;
	
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		
		try ( Transaction tx = dbManager.graphDb.beginTx() )
		{
		    Schema schema = dbManager.graphDb.schema();
		    /*IndexDefinition indexDefinition =*/ schema.indexFor( DynamicLabel.label( GENE_KEY ) )
		            .on( "locus_tag" )
		            .create();
		    tx.success();
		}
		geneLabel = DynamicLabel.label( GENE_KEY );
		
		FeatureList features = GFF3Reader.read("/home/moni/Documents/BioinformaticsMaster/Thesis/DataITM/SNP/ReferenceFiles/LD.ALL.M2.gff");
		
		try ( Transaction tx = dbManager.getGraphDb().beginTx()) {
			for (FeatureI feature : features) {
				if ("CDS".equals(feature.type())) {
					createGene(feature);
				}
			}
			tx.success();
		}
		
	}
	
	public static Node createGene(FeatureI feature) {

		String geneId = feature.getAttribute("locus_tag");
		Node node = dbManager.graphDb.createNode(geneLabel);
	    node.setProperty("locus_tag", geneId);
	    
	    String [] location = feature.location().toString().trim().replaceAll("[\\[\\]]","").replace("=",":").split(";");
	    node.setProperty( LOCATION_KEY, location);
	    node.setProperty("seqType", feature.type());
	    return node;
	}
	
/*	private static String idToGene( final String id ) {
	    return new StringBuilder("gene").append(id).toString();
	}	*/
	
}
